﻿Imports System.ComponentModel

Public Class SuperDataTimeBox

    Public Enum EnumTurno
        NIENTE = 0
        MATTINA = 1
        POMERIGGIO = 2
        SERA = 3
        NULLO = 4
    End Enum

    Public Event OnDataChange()
    Public Event OnDataClick()
    Public Event OnCalendarClick(ByRef blnProsegui As Boolean)
    Public Event OnElencaClick(ByRef sender As SuperDataTimeBox)

    Public Event OnTimerClick(ByRef blnProsegui As Boolean)
    Public Event OnTimeChange()
    Public Event OnTimeClick()

    Public DataMySql As String
    Private m_Data As String
    Private m_Time As String
    Private m_DataBox As Boolean = True
    Private m_TimeBox As Boolean = False
    Private mTurno As EnumTurno
    Private mVisibleElenca As Boolean
    Private oToolTip As New Windows.Forms.ToolTip

    Public Property Data As String
        Get
            Return m_Data
        End Get
        Set(ByVal value As String)
            If IsDate(value) Then
                m_Data = value
                DataMySql = Year(CDate(value)) & "/" & Month(CDate(value)) & "/" & Day(CDate(value))
                txtData.Text = CDate(value).ToShortDateString
            Else
                m_Data = ""
                DataMySql = ""
                If value = "" Then txtData.Text = ""
            End If
        End Set
    End Property
    Public Property Time As String
        Get
            Return m_Time
        End Get
        Set(ByVal value As String)
            If IsTime(value) Then
                m_Time = value
                txtTime.Text = value
            Else
                m_Time = ""
                If value = "" Then txtTime.Text = ""
            End If
        End Set
    End Property
    Public Property DataBox As Boolean
        Get
            Return m_DataBox
        End Get
        Set(ByVal value As Boolean)
            m_DataBox = value
            m_TimeBox = Not value
            Call SistemaPanel()
        End Set
    End Property
    Public Property TimeBox As Boolean
        Get
            Return m_TimeBox
        End Get
        Set(ByVal value As Boolean)
            m_TimeBox = value
            m_DataBox = Not value
            Call SistemaPanel()
        End Set
    End Property
    Public Property Turno As EnumTurno
        Get
            Return mTurno
        End Get
        Set(ByVal value As EnumTurno)
            mTurno = value
        End Set
    End Property
    Public Property VisibleElenca As Boolean
        Get
            Return mVisibleElenca
        End Get
        Set(ByVal value As Boolean)
            mVisibleElenca = value
            cmdElenca.Visible = mVisibleElenca
        End Set
    End Property

    Private Sub SuperDataTimeBox_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        oToolTip.SetToolTip(cmdData, "Mostra calendario")
        oToolTip.SetToolTip(cmdElenca, "Elenco date")
        oToolTip.SetToolTip(cmdTime, "Mostra orario")
        oToolTip.SetToolTip(txtData, "Valori accettati (es. 01/02/2012):" & vbCrLf & _
                                "010212     - 01022012" & vbCrLf & _
                                "01/02/12  - 01/02/2012")
        oToolTip.SetToolTip(txtTime, "Valori accettati:" & vbCrLf & _
                                " Inserito      Trasformato" & vbCrLf & _
                                "         9            09:00" & vbCrLf & _
                                "      745           07:45" & vbCrLf & _
                                "       7:5           07:05" & vbCrLf & _
                                "     1235          12:35" & vbCrLf & _
                                "     7:35           07:35")
        oToolTip.ShowAlways = True
        cmdElenca.Visible = mVisibleElenca
    End Sub

#Region "Funzioni private"

    Private Sub SistemaPanel()
        If m_DataBox Then
            Width = panData.Width
        Else
            Width = panTime.Width
        End If
        panData.Visible = m_DataBox
        panTime.Visible = Not m_DataBox
        Height = panData.Height
    End Sub

    Private Sub SistemaData()
        Try
            Dim intNumeroCaratteri As Integer = Len(txtData.Text)
            Dim strData As String = ""

            If InStr(1, txtData.Text, "/") <> 0 Then
                Select Case intNumeroCaratteri
                    Case 10
                        strData = txtData.Text
                    Case 8
                        strData = Mid(txtData.Text, 1, 6) & IIf(Int(Mid(txtData.Text, 7, 2)) > 30, 19, 20) & Mid(txtData.Text, 7, 2)
                    Case Else
                        strData = txtData.Text
                End Select
            Else
                Select Case intNumeroCaratteri
                    Case 6
                        strData = Mid(txtData.Text, 1, 2) & "/" & Mid(txtData.Text, 3, 2) & "/" & IIf(Int(Mid(txtData.Text, 5, 2)) > 30, 19, 20) & Mid(txtData.Text, 5, 2)
                    Case 8
                        strData = Mid(txtData.Text, 1, 2) & "/" & Mid(txtData.Text, 3, 2) & "/" & Mid(txtData.Text, 5, 4)
                End Select
            End If
            If IsDate(strData) Then
                txtData.Text = strData
            Else
                txtData.Text = ""
            End If
        Catch ex As Exception
            Base.ShowError.Show("Sistema Data", Me.Name, "SistemaData", ex)
        End Try
    End Sub

    Private Sub SistemaTime()
        Try
            Dim v_valori() As String
            Dim strTime As String
            Dim intNumeroOccorrenze As Integer
            Dim i As Integer
            For i = 1 To Len(txtTime.Text)
                If Mid(txtTime.Text, i, 1) = ":" Then intNumeroOccorrenze = intNumeroOccorrenze + 1
            Next i

            strTime = ""
            Select Case Len(txtTime.Text)
                Case 5
                    If intNumeroOccorrenze = 1 Then
                        v_valori = Split(txtTime.Text, ":")
                        If IsNumeric(v_valori(0)) And IsNumeric(v_valori(1)) Then
                            If v_valori(0) <= 23 And v_valori(1) <= 59 Then
                                strTime = txtTime.Text
                            End If
                        End If
                    End If
                Case 4
                    Select Case intNumeroOccorrenze
                        Case 0
                            If IsNumeric(txtTime.Text) Then
                                If Mid(txtTime.Text, 1, 2) <= 23 And Mid(txtTime.Text, 3, 2) <= 59 Then
                                    strTime = Format(CInt(Mid(txtTime.Text, 1, 2)), "00") & ":" & Format(CInt(Mid(txtTime.Text, 3, 2)), "00")
                                End If
                            End If
                        Case 1
                            v_valori = Split(txtTime.Text, ":")
                            If IsNumeric(v_valori(0)) And IsNumeric(v_valori(1)) Then
                                If v_valori(0) <= 23 And v_valori(1) <= 59 Then
                                    strTime = Format(v_valori(0), "00") & ":" & Format(v_valori(1), "00")
                                End If
                            End If
                    End Select
                Case 3
                    If IsNumeric(txtTime.Text) Then
                        If Mid(txtTime.Text, 2, 2) <= 59 Then
                            strTime = Format(CInt(Mid(txtTime.Text, 1, 1)), "00") & ":" & Format(CInt(Mid(txtTime.Text, 2, 2)), "00")
                        End If
                    End If
                Case 2, 1
                    If IsNumeric(txtTime.Text) Then
                        If Int(txtTime.Text) <= 23 Then
                            strTime = Format(CInt(txtTime.Text), "00") & ":00"
                        End If
                    End If
            End Select

            If IsTime(strTime) Then
                txtTime.Text = strTime
            Else
                txtTime.Text = ""
            End If
        Catch ex As Exception
            Base.ShowError.Show("Sistema time", Me.Name, "SistemaTime", ex)
        End Try
    End Sub

    Private Function IsTime(ByVal inValore As String) As Boolean
        Dim v_valori() As String
        Dim intNumeroOccorrenze As Integer
        Dim i As Integer
        For i = 1 To Len(inValore)
            If Mid(inValore, i, 1) = ":" Then intNumeroOccorrenze = intNumeroOccorrenze + 1
        Next i

        Select Case Len(inValore)
            Case 5
                If intNumeroOccorrenze = 1 Then
                    v_valori = Split(inValore, ":")
                    If IsNumeric(v_valori(0)) And IsNumeric(v_valori(1)) Then
                        If v_valori(0) <= 23 And v_valori(1) <= 59 Then
                            IsTime = True
                        Else
                            IsTime = False
                        End If
                    Else
                        IsTime = False
                    End If
                Else
                    IsTime = False
                End If
            Case 4
                Select Case intNumeroOccorrenze
                    Case 0
                        If IsNumeric(inValore) Then
                            If Mid(inValore, 1, 2) <= 23 And Mid(inValore, 3, 2) <= 59 Then
                                IsTime = True
                            Else
                                IsTime = False
                            End If
                        Else
                            IsTime = False
                        End If
                    Case 1
                        v_valori = Split(inValore, ":")
                        If IsNumeric(v_valori(0)) And IsNumeric(v_valori(1)) Then
                            If v_valori(0) <= 23 And v_valori(1) <= 59 Then
                                IsTime = True
                            Else
                                IsTime = False
                            End If
                        Else
                            IsTime = False
                        End If
                    Case Else
                        IsTime = False
                End Select
            Case 3
                If IsNumeric(inValore) Then
                    If Mid(inValore, 2, 2) <= 59 Then
                        IsTime = True
                    Else
                        IsTime = False
                    End If
                Else
                    IsTime = False
                End If
            Case 2, 1
                If IsNumeric(inValore) Then
                    If Int(inValore) <= 23 Then
                        IsTime = True
                    Else
                        IsTime = False
                    End If
                Else
                    IsTime = False
                End If
            Case Else
                IsTime = False
        End Select
    End Function

#End Region

#Region "Funzioni publiche"

    Public Sub Pulisci()
        If m_DataBox Then
            txtData.Text = ""
        Else
            txtTime.Text = ""
        End If
    End Sub

    Public Sub EnableElenca(ByVal inEnable As Boolean)
        cmdElenca.Enabled = inEnable
    End Sub

#End Region

#Region "Eventi"

    Private Sub txtData_Click() Handles txtData.Click
        RaiseEvent OnDataClick()
    End Sub

    Private Sub txtTime_Click() Handles txtTime.Click
        RaiseEvent OnTimeClick()
    End Sub

    Private Sub txtData_Change(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtData.TextChanged
        If IsDate(txtData.Text) Then
            m_Data = txtData.Text
            DataMySql = Year(CDate(txtData.Text)) & "/" & Month(CDate(txtData.Text)) & "/" & Day(CDate(txtData.Text))
        Else
            m_Data = ""
            DataMySql = ""
        End If
        RaiseEvent OnDataChange()
    End Sub

    Private Sub txt_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtData.GotFocus, txtTime.GotFocus
        CType(sender, SuperTextBox).BackColor = Drawing.Color.LemonChiffon
        CType(sender, SuperTextBox).SetFocusSelected()
    End Sub

    Private Sub txtData_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtData.KeyPress
        Select Case e.KeyChar
            Case "-"
                e.KeyChar = "/"
            Case " "
                e.KeyChar = "/"
            Case Else
                If Not (e.KeyChar = "/" Or IsNumeric(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13) Then
                    e.KeyChar = ""
                    Beep()
                End If
        End Select
    End Sub

    Private Sub txt_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtData.LostFocus, txtTime.LostFocus
        CType(sender, SuperTextBox).BackColor = Drawing.Color.White
        If m_TimeBox Then
            Call SistemaTime()
        Else
            Call SistemaData()
        End If
    End Sub

    Private Sub txtTime_Change(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtTime.TextChanged
        If IsTime(txtTime.Text) Then
            m_Time = txtTime.Text
        Else
            m_Time = ""
        End If
        RaiseEvent OnTimeChange()
    End Sub

    Private Sub txtTime_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTime.KeyPress
        Select Case e.KeyChar
            Case "-"
                e.KeyChar = ":"
            Case " "
                e.KeyChar = ":"
            Case Else
                If Not (e.KeyChar = ":" Or IsNumeric(e.KeyChar) Or Asc(e.KeyChar) = 8 Or Asc(e.KeyChar) = 13) Then
                    e.KeyChar = ""
                    Beep()
                End If
        End Select
    End Sub

    Private Sub txt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTime.KeyDown, txtData.KeyDown
        Select Case e.KeyCode
            Case Windows.Forms.Keys.Return
                If e.Handled = False Then
                    e.Handled = True
                    Windows.Forms.SendKeys.Send("{TAB}")
                End If
                e.SuppressKeyPress = True
            Case Windows.Forms.Keys.Escape
                e.SuppressKeyPress = True
        End Select
    End Sub

#End Region

#Region "Comandi"

    Private Sub cmdData_OnClickOrPress(ByRef sender As SuperSmallButton) Handles cmdData.OnClickOrPress, cmdTime.OnClickOrPress
        Dim blnProsegui As Boolean = True

        If m_TimeBox Then
            RaiseEvent OnTimerClick(blnProsegui)
            If blnProsegui Then
                Dim ofrmOrario As New frmTime
                If ofrmOrario.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Time = ofrmOrario.strTime
                End If
                ofrmOrario = Nothing
            End If
        Else
            RaiseEvent OnCalendarClick(blnProsegui)
            If blnProsegui Then
                Dim ofrmCalendario As New frmCalendar
                If ofrmCalendario.ShowDialog = Windows.Forms.DialogResult.OK Then
                    Data = ofrmCalendario.strData
                End If
                ofrmCalendario = Nothing
            End If
        End If
    End Sub

    Private Sub cmdElenca_OnClickOrPress(ByRef sender As SuperSmallButton) Handles cmdElenca.OnClickOrPress
        RaiseEvent OnElencaClick(Me)
    End Sub

#End Region

End Class
